
$(document).ready(function(){
    authorExists = false;
    bookExists = false;
    
    //Displays all of the books that the user has read in the div on the right
    $('#viewReadBooks').live('click', 
        function(){
           
            while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );
            
            $.post("DBServlet", {                                               //ask servlet for all read books
                action: "findReadBooks"
            }, function(data){
                alert(data);
                var books = $(data).find("books");
                var newdiv = document.createElement('div');
                newdiv.innerHTML = "";
                var el = "<table border='0'><tr><td>remove</td><td>Title</td><td>Author</td></tr>";
                var id = 0;
                var row = 'row1';
                books.each( function(){                                         //Print the books that the loged in user have read
                    el = el + "<tr id='"+id+"' class='"+row+"'>";
                    var title = $(this).find('title').text();
                    var name = $(this).find('name').text();
                    var authorid = $(this).find('authorId').text();
                    var bookid = $(this).find('bookId').text();
                    el = el + "<td><a href='#' onclick='removeReadBook( " + bookid + " , " + id + " )'> X </a></td>" ;
                    el = el + "<td><a href='DisplayServlet?type=book&id=" + bookid +"'>" + title + "</a></td>" ;
                    el = el + "<td><a href='DisplayServlet?type=author&id=" + authorid + "'>" + name + "</a></td>"
                    el = el + "</tr>";
                    if(row=="row1"){
                        row = "row2";
                    }else{
                        row = 'row1';
                    }
                    id = id+1;
                });
                el = el + "</table>";
                newdiv.innerHTML = el;
                viewDiv.appendChild(newdiv);          
            }, "text");
        

        });
    
    //Creates the form to add a new book
    $('#addBook').live('click', 
          
        function (){
            while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );
       
            var newdiv = document.createElement('div');                         //creating a div to put imputs in to be able to add book
            newdiv.innerHTML = 
            "<form action='DBServlet' method='POST'>" +
            "<input type='hidden' name='action' value='addBook'/>" +
            "Book Title " + " <br><input type='text' id='title' name='title'><br>" +
            "Author " + "<br><input type='text' id ='author' name='author'><br>" +
            "Synopsis " + "<br><textarea rows='10' name='synopsis'></textarea><br>" +
            "Orginal Language " + "<br><input type='text' name='language'><br>" +
            "Year of release " + "<br><input type='text' name='year'><br>" +
            "Publicher " + "<br><input type='text' name='publicher'><br>" +
            "<input disabled='true' type='submit' name='addBook' id ='addBookBtn' value='add book'>" +
            "</form>"
            
            viewDiv.appendChild(newdiv);
            $('#title').blur(function() {                                       //when cursor exit title input 
                title = $('#title').attr('value');
                
                $.post("DBServlet", {                                           //ask servlet if book exists
                    action: "findBook", 
                    what: title
                }, function(data){
                    if(data == "true"){                                         
                        alert("Book already exist");                            //check answer from the servlet
                        bookExists = true;
                    }else{
                        bookExists = false;
                    }
                    
                    if(authorExists && !bookExists){                            
                        $('#addBookBtn').attr('disabled', false);               //author exists, book doesn't exist, enable add book button
                    }else{
                        $('#addBookBtn').attr('disabled', true);
                    }
                }, "text");
            });
            
            $('#author').blur(function() {                                      //when cursor exits author input 
                author = $('#author').attr('value');                            //ask servlet if author exist
                $.post("DBServlet", {
                    action: "findAuthor", 
                    what: author
                }, function(data){
                    if(data == "true"){                                         //checks answer from servlet
                        authorExists = true;
                    }else{
                        alert("Author doesn't exist");
                        authorExists = false;
                    }
                    
                    if(authorExists && !bookExists){                            //author exist book don't                       
                        $('#addBookBtn').attr('disabled', false);               //enable add book button
                    }else{
                        $('#addBookBtn').attr('disabled', true);                //disable add book button
                    }
                }, "text");   
                      
            });
        });
    
    //Creates a form to add a new author
    $('#addAuthor').live('click', 
        function(){

            while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );

            var newdiv = document.createElement('div');                         //creating a div to put imputs in to be able to add author
            newdiv.innerHTML = 
            "<form action='DBServlet' method='POST'>" +
            "<input type='hidden' name='action' value='addAuthor'/>" +
            "Name " + " <br><input type='text' id = 'name' name='name'><br>" +
            "Country " + "<br><input type='text' name='country'><br>" +
            "Biography " + "<br><textarea rows='10' name='biography'></textarea><br>" +
            "WebSite " + "<br><input type='text' name='webSite'><br>" +
            "<input disabled='true' type='submit' id ='addAuthorBtn' name='addAuthor' value='add author'>" +
            "</form>"
            viewDiv.appendChild(newdiv);
            $('#name').blur(function() {
                author = $('#name').attr('value');
                $.post("DBServlet", {                                           //ask servet if author exists
                    action: "findAuthor", 
                    what: author
                }, function(data){
                    if(data == "true"){                                         //checks answer from the servlet
                        $('#addAuthorBtn').attr('disabled', true);
                        alert("Author already exist");                          //author already exists, alert user
                    }else{                                                      
                        $('#addAuthorBtn').attr('disabled', false);             //author doesn't exist, enable button add author
                    }
                }, "text");
                 
            });
        });
        
    $('#alterBook').live('click', 
        function(){
            while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );

            var newdiv = document.createElement('div');                         //creating a div to put imputs in to be able to add author
            newdiv.innerHTML = 
            "<form " +
            "<td>Title</td>" +
            " <br><input type='text' id ='title' name='title'><br>" +
            "<input type='button' onClick='ashow()' value='search' />" +
            "</form>"

            viewDiv.appendChild(newdiv);
            $('#title').blur(function() {
                book = $('#title').attr('value');
                $.post("DBServlet", {                                           
                    action: "findBookLike", 
                    what: book
                }, function(data){
                    var books = $(data).find("books");
           
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "";
                    var el = "<table border='0'><tr><td>Title</td><td>Author</td></tr>";
                    var id = 0;
                    var row = 'row1';
                    books.each( function(){                                         //Print the books that the loged in user have read
                        el = el + "<tr id='"+id+"' class='"+row+"'>";
                        var title = $(this).find('title').text();
                        var name = $(this).find('name').text();
                        var bookid = $(this).find('bookId').text();
                        el = el + "<td><a href='#' onclick='displayForAlter( " + bookid + " )'> " + title + " </a></td>" ;
                        el = el + "<td><a>" + name + "</a></td>"
                        el = el + "</tr>";
                        if(row=="row1"){
                            row = "row2";
                        }else{
                            row = 'row1';
                        }
                        id = id+1;
                    });
                    el = el + "</table>";
                    newdiv.innerHTML = el;
                    viewDiv.appendChild(newdiv); 
                }, "text");
                 
            });
             
        });
        
    $('#alterAuthor').live('click', 
        function(){
            while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );

            var newdiv = document.createElement('div');                         //creating a div to put imputs in to be able to add author
            newdiv.innerHTML = 
            "<form " +
            "<td>Name</td>" +
            " <br><input type='text' id ='name' name='name'><br>" +
            "<input type='button' onClick='ashow()' value='search' />" +
            "</form>"

            viewDiv.appendChild(newdiv);
            $('#name').blur(function() {
                author = $('#name').attr('value');
                $.post("DBServlet", {                                           
                    action: "findAuthorLike", 
                    what: author
                }, function(data){
                    var authors = $(data).find("authors");
           
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "";
                    var el = "<table border='0'><tr><td>Name</td><td>Web Site</td></tr>";
                    var id = 0;
                    var row = 'row1';
                    authors.each( function(){                  
                        el = el + "<tr id='"+id+"' class='"+row+"'>";
                        var authorid = $(this).find('authorId').text();
                        var name = $(this).find('name').text();
                        var web = $(this).find('webSite').text();
                        el = el + "<td><a href='#' onclick='displayForAlterAuthor( " + authorid + " )'> " + name + " </a></td>" ;
                        el = el + "<td><a>" + web + "</a></td>"
                        el = el + "</tr>";
                        if(row=="row1"){
                            row = "row2";
                        }else{
                            row = 'row1';
                        }
                        id = id+1;
                    });
                    el = el + "</table>";
                    newdiv.innerHTML = el;
                    viewDiv.appendChild(newdiv); 
                }, "text");
                 
            });
        });
    
    $('#removeBook').live('click', 
        function(){
        
            while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );

            var newdiv = document.createElement('div');                         //creating a div to put imputs in to be able to add author
            newdiv.innerHTML = 
            "<form " +
            "<td>Title</td>" +
            "<br><input type='text' id ='title' name='title'><br>" +
            "<td><a href='#' onclick='removeBook()'> Get book </a></td>" +
            "</form>"
            viewDiv.appendChild(newdiv);
            $('#title').blur(function() {
                book = $('#title').attr('value');
                $.post("DBServlet", {                                           
                    action: "findBookLike", 
                    what: book
                }, function(data){
                    var books = $(data).find("books");
           
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "";
                    var el = "<table border='0'><tr><td>Title</td><td>Author</td></tr>";
                    var id = 0;
                    var row = 'row1';
                    books.each( function(){                                         //Print the books that the loged in user have read
                        el = el + "<tr id='"+id+"' class='"+row+"'>";
                        var title = $(this).find('title').text();
                        var name = $(this).find('name').text();
                        var bookid = $(this).find('bookId').text();
                        el = el + "<td><a href='#' onclick='removeBook( " + bookid + " , " + id + " )'> " + title + " </a></td>" ;
                        el = el + "<td><a>" + name + "</a></td>"
                        el = el + "</tr>";
                        if(row=="row1"){
                            row = "row2";
                        }else{
                            row = 'row1';
                        }
                        id = id+1;
                    });
                    el = el + "</table>";
                    newdiv.innerHTML = el;
                    viewDiv.appendChild(newdiv); 
                }, "text");
                 
            });
        
        });
    
    $('#removeAuthor').live('click', 
        function(){
        
            while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );

            var newdiv = document.createElement('div');                         //creating a div to put imputs in to be able to add author
            newdiv.innerHTML = 
            "<form " +
            "<td>Name</td>" +
            "<br><input type='text' id ='name' name='name'><br>" +
            "<td><a href='#' onclick='???'> Get book </a></td>" +
            "</form>"
            viewDiv.appendChild(newdiv);
            $('#name').blur(function() {
                author = $('#name').attr('value');
                $.post("DBServlet", {                                           
                    action: "findAuthorLike", 
                    what: author
                }, function(data){
                    var authors = $(data).find("authors");
           
                    var newdiv = document.createElement('div');
                    newdiv.innerHTML = "";
                    var el = "<table border='0'><tr><td>Author</td></tr>";
                    var id = 0;
                    var row = 'row1';
                    authors.each( function(){                                         //Print the books that the loged in user have read
                        el = el + "<tr id='"+id+"' class='"+row+"'>";
                        var name = $(this).find('name').text();
                        var authoriId = $(this).find('authorId').text();
                        el = el + "<td><a href='#' onclick='removeAuthor( " + authoriId + " , " + id + " )'> " + name + " </a></td>" ;
                        el = el + "</tr>";
                        if(row=="row1"){
                            row = "row2";
                        }else{
                            row = 'row1';
                        }
                        id = id+1;
                    });
                    el = el + "</table>";
                    newdiv.innerHTML = el;
                    viewDiv.appendChild(newdiv); 
                }, "text");
                 
            });
        });
        
});

function removeReadBook( bookid , elementid ){
    $.post("DBServlet", {                                                       //remove book from the users list of read books.
        action: "removeReadBook" , 
        book: bookid.toString()
    }, function(data) {
        //     alert(data);
        if(data == "true" ){
            $("#"+elementid).remove();
        }
    })
}
       
function displayForAlter(bookId){
    while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );

    $.post("DBServlet", {                                               
        action: "getBook", 
        what: bookId.toString()
    }, function(data){
        var book = $(data).find("book");
        var title = $(book).find('title').text();
        var author = $(book).find('name').text();
        alert("author: " + author);
        var bookid = $(book).find('bookId').text();
        var firstRelease = $(book).find('firstRelease').text();
        var originalLanguage = $(book).find('originalLanguage').text();
        var publisher = $(book).find('publisher').text();
        var synopsis = $(book).find('synopsis').text();
                 
        while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );
       
        var newdiv = document.createElement('div');                         
        newdiv.innerHTML = 
        "<form action='DBServlet' method='POST'>" +
        "<input type='hidden' name='action' value='alterBook'/>" +
        "Book id " + " <br><input value=" + bookid + " type='text' id='bookid' name='bookid' readonly><br>" + 
        "Book Title " + " <br><input value=" + title + " type='text' id='title' name='title'><br>" +
        "Author " + "<br><input value=" + author + " type='text' id ='author' name='author'><br>" +
        "Synopsis " + "<br><textarea rows='10' type='text' name='synopsis'>" + synopsis + "</textarea><br>" +
        "Orginal Language " + "<br><input value=" + originalLanguage + " type='text' name='language'><br>" +
        "Year of release " + "<br><input value=" + firstRelease + " type='text' name='year'><br>" +
        "Publicher " + "<br><input value=" + publisher + " type='text' name='publicher'><br>" +
        "<input type='submit' name='alterBook' id ='alterBookBtn' value='alter book'>" +
        "</form>"
            
        viewDiv.appendChild(newdiv);
        $('#author').blur(function() {
            author = $('#author').attr('value');
            $.post("DBServlet", {                                           //ask servet if author exists
                action: "findAuthor", 
                what: author
            }, function(data){
                if(data == "true"){                                         //checks answer from the servlet
                    $('#alterBookBtn').attr('disabled', false);
                }else{                                                      
                    $('#alterBookBtn').attr('disabled', true);              //author doesn't exist, enable button add author
                    alert("Author do not exist");  
                }
            }, "text");
                 
        });
    }, "text");
}

function displayForAlterAuthor(authorId){
    while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );

    $.post("DBServlet", {                                               
        action: "getAuthor", 
        what: authorId.toString()
    }, function(data){
        var author = $(data).find("author");
        var name = $(author).find('name').text();
        var country = $(author).find('country').text();
        var biography = $(author).find('biography').text();
        var webSite = $(author).find('webSite').text();
                 
        while ( viewDiv.firstChild ) viewDiv.removeChild( viewDiv.firstChild );
       
        var newdiv = document.createElement('div');                         
        newdiv.innerHTML = 
        "<form action='DBServlet' method='POST'>" +
        "<input type='hidden' name='action' value='alterAuthor'/>" +
        "Author id " + " <br><input value=" + authorId + " type='text' id='authorid' name='authorid' readonly><br>" + 
        "Name " + " <br><input value=" + name + " type='text' id='name' name='name'><br>" +
        "Country " + "<br><input value=" + country + " type='text' id ='country' name='country'><br>" +
        "Biography " + "<br><textarea rows='10' value=" + biography + " type='text' name='biography'></textarea><br>" +
        "WebSite " + "<br><input value=" + webSite + " type='text' name='webSite'><br>" +
        "<input type='submit' name='alterAuthor' id ='alterAuthorBtn' value='alter author'>" +
        "</form>"
            
        viewDiv.appendChild(newdiv);

    }, "text");
}
    
function removeBook(bookid , id){
    $.post("DBServlet", {                                           
        action: "getBook", 
        what: bookid.toString()
    }, function(data){
        var book = $(data).find("book");
        var title = $(book).find('title').text();
        var x = confirm("Are you sure you want to remove " + title + "?");
        if (x){
            $.post("DBServlet", {                                                      
                action: "removeBook" , 
                what: bookid.toString()
            }, function(data) {
                if(data == "true" ){
                    $("#"+id).remove();
                }else{
                    alert("The book could not be removed");
                }
            })
        }else{
    }
    }, "text");
}

function removeAuthor( authorid , id ){

    $.post("DBServlet", {                                           
        action: "getAuthor", 
        what: authorid.toString()
    }, function(data){
        alert(data);
        var author = $(data).find("author");
        var name = $(author).find('name').text();
        var x = confirm("Are you sure you want to remove " + name + "?");
        if (x){
            $.post("DBServlet", {                                                       
                action: "removeAuthor" , 
                what: authorid.toString()
            }, function(data) {
                if(data == "true" ){
                    $("#"+id).remove();
                }else{
                    alert("The author could not be removed");
                }
            })
        }else{
    }
    }, "text");
}

function show( book ){
    alert("show " + book);
}
    
